﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ProjControleModelos;
namespace ProjControleDAL
{
   public class ServicosDAL
    {
       Dados objD = null;
      SqlCommand cmd = null;

      public ServicosDAL()
      {
      }
        #region Métodos

      //inserir
      public void inserir(Servicos objS)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "insert into servicos values(@descricao,@val_servico)";
              cmd.Parameters.Add(new SqlParameter("@descricao", SqlDbType.VarChar)).Value = objS.descricao_servico;
              cmd.Parameters.Add(new SqlParameter("@val_servico", SqlDbType.Float)).Value = objS.val_servico;
              objS.cod_servico = objD.executaComandoAI(cmd, "servicos");
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          
      }
      //alterar
      public void alterar(Servicos objS)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "update servicos set descricao_servico = @descricao, val_servico = @val_servico where cod_servico = @codigo";
              cmd.Parameters.Add(new SqlParameter("@descricao", SqlDbType.VarChar)).Value = objS.descricao_servico;
              cmd.Parameters.Add(new SqlParameter("@val_servico", SqlDbType.Float)).Value = objS.val_servico;
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = objS.cod_servico;
              objD.executaComando(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
      }
      public void excluir(Servicos objS)
      {
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "delete from servicos where cod_servico = @codigo";
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = objS.cod_servico;
              objD.executaComando(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
      }
      public Servicos localizar(int codigo)
      {
          Servicos objS = null;
          DataTable tab;
          try
          {
              cmd = new SqlCommand();
              objD = new Dados();
              cmd.CommandText = "select * from servicos where cod_servico = @codigo";
              cmd.Parameters.Add(new SqlParameter("@codigo", SqlDbType.Int)).Value = codigo;
              tab = objD.executaConsulta(cmd);
              if(tab != null)
                  if (tab.Rows.Count == 1)
                  {
                      objS = new Servicos(codigo, tab.Rows[0]["descricao_servico"].ToString(),
                         Double.Parse(tab.Rows[0]["val_servico"].ToString()));
                  }
              
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          return objS;
      }
      public DataTable localizar()
      {
          
          DataTable tab;
          try
          {
              objD = new Dados();
              cmd = new SqlCommand();
              cmd.CommandText = "select * from servicos order by descricao_servico";
              tab = objD.executaConsulta(cmd);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
          cmd = null;
          objD = null;
          return tab;
      }
        #endregion
    }
}
